#include "ToolBox/physic/physic.h"

Physic::Physic(Vectorf * obj)
{
    objectPosition=obj;
    frottement=0;
    velocityMax=10;

    gravity=1;
    valGravity=-50.8;

    velocity = new Vectorf(0.0, 0.0, 0.0);
    delta=0.015;            //  15 milliseconde par rafraichissement de l'image

}

int Physic::SetFrottement()
{
    frottement=1;
}

int Physic::AddAcceleration(Vectorf *vec)
{
    pile.push(vec);
}

int Physic::AddAccelerationc(Vectorf *vec, float coef)
{
    pile.push(new Vectorf(*vec*coef));
}

void Physic::Do()
{
    if(gravity==1)
    {
        pile.push(new Vectorf(0.0,valGravity,0.0));
    }

    Vectorf * accelerationFinale = new Vectorf(0.0, 0.0, 0.0);

    while(pile.size()!=0)
    {
        *accelerationFinale=*accelerationFinale+(*(pile.top()));
        pile.pop();
    }

    Vectorf vitesseA= (*accelerationFinale*delta);
    *velocity=*velocity+vitesseA;

    if(frottement)
    {
        Vectorf vectemp=*velocity;
        vectemp=vectemp.Normalize();
        vectemp=vectemp*0.40;
        *velocity=*velocity-vectemp;

        if(velocity->Norme()>velocityMax)
        {
            Vectorf vectemp=velocity->Normalize();
            vectemp=vectemp*velocityMax;
            *velocity=vectemp;
        }
    }
    else if (velocity->Norme()<0.1)
    {
        velocity=new Vectorf();
    }

    *objectPosition= *objectPosition+(*velocity*delta);
    frottement=0;
}

int Physic::UnsetFrottement()
{
    frottement=0;
}
